/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.fedoraaccess.rest;

import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import cz.mzk.rehan.commons.DatastreamId;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
class GetXmlDatastreamDisseminationOperation extends AbstractOperation {

    private static final Logger logger = Logger.getLogger(GetXmlDatastreamDisseminationOperation.class.getName());

    public GetXmlDatastreamDisseminationOperation(FedoraServer fedoraServer) {
        super(new HttpConnector(fedoraServer, HttpMethod.GET));
    }

    public Document run(String pid, DatastreamId dsId) {
        String request = buildRequest(pid, dsId);
        logger.log(Level.FINE, "request: ''{0}''", request);
        try {
            return connector.processRequestWithXmlResult(request);
        } catch (DocumentException ex) {
            logger.log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
            return null;
        }
    }

    private String buildRequest(String pid, DatastreamId datastreamId) {
        StringBuilder builder = new StringBuilder();
        builder.append(getBaseUrl());
        builder.append("/objects/");
        builder.append(pid);
        builder.append("/datastreams/");
        builder.append(datastreamId.toString());
        builder.append("/content");
        return builder.toString();
    }
}
